package com.tool_stdy.concurrent;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.*;

/**
 * Created by yan_li on 2016/1/26.
 */
public class ConcurrentLinkedQueueTest {
        private static ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue();
        private static CountDownLatch latch1 = new CountDownLatch(5);
        static ExecutorService pool = Executors.newFixedThreadPool(2);

        public static void getNums(){
            for(int i=0;i<5;i++){
                pool.submit(new Runnable() {
                    @Override
                    public void run() {
                        while (!queue.isEmpty()) {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            //queue.poll();
                            System.out.println(Thread.currentThread().getName()+"==="+queue.poll());
                        }
                        latch1.countDown();
                    }
                });
            }
        }

        public static void getNums2(){
            for(int i=0;i<5;i++){
                pool.submit(new Runnable() {
                    @Override
                    public void run() {
                        while (!queue.isEmpty()) {
                            System.out.println(Thread.currentThread().getName()+"==="+queue.peek());
                        }
                        latch1.countDown();
                    }
                });
            }
        }

        public static void main(String[] args) throws UnsupportedEncodingException, ExecutionException, InterruptedException {
            for(int i=0;i<=2000;i++){
                queue.offer(i);
            }

            new Exception("d");
            System.out.println("=====>"+queue.size());
            getNums();
            Thread.sleep(10);
            System.out.println("=====>111=="+((ThreadPoolExecutor) pool).getQueue().size());
            System.out.println(queue.size());
            latch1.await();
            System.out.println(queue.size());
            pool.shutdown();
        }
    }